<?php
namespace App\Controller;

class ThreadsController extends AppController{

    public $displayNum = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.18
   * ps：获取帖子内容
   * */
    public function getInfo()
    {
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $data = $this->Threads->findById($id)->first();
        if(empty($data)){
            $this->G->error("thread_not_found");
            return null;
        }
        $this->loadComponent('TextareaCache');
        $content = $this->TextareaCache->getVal('threads', $id);
        $this->loadComponent('ContentFormat');
        $data['content']  = empty($content) ? '' : $this->ContentFormat->add($content['content']);
        $this->G->success('Success', $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.08
   * ps：修改帖子
   * */
    public function update()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $title = $this->request->getData("title");
        $content = $this->request->getData("content");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        if(empty($content)){
            $this->G->error("content_error");
            return null;
        }
        $this->loadComponent('ThreadCache');
        $data = $this->Threads->findById($id)->first();
        if(empty($data)){
            $this->G->error("thread_not_found");
            return null;
        }
        $thread = $this->Threads->newEntity();
        $thread->id = $id;
        $thread->title = $title;
        $thread_save = $this->Threads->save($thread);
        if(!$thread_save){
            $this->G->error("save_error");
            return null;
        }
        $this->loadComponent('TextareaCache');
        $this->loadComponent('ContentFormat');
        $content  = $this->ContentFormat->clean($content)[0];
        $this->TextareaCache->update('threads', $id, $content);
        $this->ThreadCache->deleteCache($id);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.08
   * ps：获取帖子列表
   * */
    public function index()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $data  = $this->request->getData();
        $this->loadComponent('ThreadCache');
        $this->loadComponent('UserCache');
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $list = $this->ThreadCache->getList($data);
        $data = [];
        $data['pageTotal'] = 0;
        $data['list'] = [];
        if(empty($list[$key]['list'])){
            $this->G->success('Success',$data);
            return null;
        }
        $data['pageTotal'] = ceil($list[$key]['total']/$this->displayNum);
        foreach ($list[$key]['list'] as $k=>$v){
            $thread = $this->ThreadCache->getVal($v);
            if(empty($thread)){
                continue;
            }
            $user = $this->UserCache->getVal($thread['user_id'], null, 'UserInfos');
            if(empty($user)){
                continue;
            }
            $data['list'][$k]['nickname'] = $user['nickname'];
            $data['list'][$k]['id'] = $v;
            $data['list'][$k]['title'] = $thread['title'];
            $data['list'][$k]['add_time'] = date('Y-m-d', $thread['add_time']);
            $data['list'][$k]['expert'] = $thread['expert'];
            $data['list'][$k]['essence'] = $thread['essence'];
            $data['list'][$k]['display'] = $thread['display'];
            $data['list'][$k]['enabled'] = $thread['enabled'];
            $data['list'][$k]['order_num'] = $thread['order_num'];
        }
        array_values($data['list']);
        $this->G->success('Success', $data);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.08
   * ps：删除帖子
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->loadComponent('ThreadCache');
        $cache = $this->ThreadCache->getVal($id);
        if(empty($cache)){
            $this->G->error("not_found");
            return null;
        }
        $this->Threads->deleteAll(['id'=>$id]);
        $this->loadComponent('TextareaCache');
        $this->TextareaCache->delete('threads', $id);
        $this->ThreadCache->deleteCache($id);
        $this->ThreadCache->deleteList();
        $this->G->success('Success');
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：设置可用/不可用
    * */
    public function setEnabled()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        $enabled = $this->request->getData("enabled");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        if($enabled != 0 && $enabled != 1){
            $this->G->error("enabled_error");
            return null;
        }
        $this->Threads->query()
            ->update()
            ->set(['enabled'=>$enabled])
            ->where(['id in' => $ids])
            ->execute();
        $this->loadComponent('ThreadCache');
        foreach ($ids as $k=>$v){
            $this->ThreadCache->deleteCache($v);
        }
        $this->ThreadCache->deleteList();
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：设置排序
    * */
    public function setSort()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        $order_num = $this->request->getData("order_num");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(!is_numeric($order_num)){
            $this->G->error("order_num_error");
            return null;
        }
        $this->Threads->query()
            ->update()
            ->set(['order_num'=>$order_num])
            ->where(['id' => $id])
            ->execute();
        $this->loadComponent('ThreadCache');
        $this->ThreadCache->deleteCache($id);
        $this->ThreadCache->deleteList();
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：设置显示/不显示
    * */
    public function setDisplay()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        $display = $this->request->getData("display");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        if($display != 0 && $display != 1){
            $this->G->error("display_error");
            return null;
        }
        $this->Threads->query()
            ->update()
            ->set(['display'=>$display])
            ->where(['id in' => $ids])
            ->execute();
        $this->loadComponent('ThreadCache');
        foreach ($ids as $k=>$v){
            $this->ThreadCache->deleteCache($v);
        }
        $this->ThreadCache->deleteList();
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：设置精华
    * */
    public function setEssence()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        $essence = $this->request->getData("essence");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        if($essence != 0 && $essence != 1){
            $this->G->error("essence_error");
            return null;
        }
        $this->Threads->query()
            ->update()
            ->set(['essence'=>$essence])
            ->where(['id in' => $ids])
            ->execute();
        $this->loadComponent('ThreadCache');
        foreach ($ids as $k=>$v){
            $this->ThreadCache->deleteCache($v);
        }
        $this->ThreadCache->deleteList();
        $this->G->success("Success");
    }
}
?>